﻿using Atom.dao.Model;
using System;
using System.Collections.Generic;
using System.Text;

namespace Protocol
{
    public class ResOnline
    {
        public bool HasUser;            //是否拥有账号
        public int ID;                  //用户ID
        public int AccountID;           //账号ID
        public string Name;             //用户名
        public int Level;               //等级
        public int Exp;                 //经验值
        public int WinCount;            //胜利次数
        public int LoseCount;           //失败次数
        public int RunCount;            //逃跑次数

        public List<int> HeroList = new List<int>();  //英雄列表    

        public byte[] Encode()
        {
            byte[] buffer = null;
            using (MemoryStreamExt ms = new MemoryStreamExt())
            {
                if (HasUser)
                {
                    ms.WriteBool(true);
                    ms.WriteInt(ID);
                    ms.WriteInt(AccountID);
                    ms.WriteUTF8String(Name);
                    ms.WriteInt(Level);
                    ms.WriteInt(Exp);
                    ms.WriteInt(WinCount);
                    ms.WriteInt(LoseCount);
                    ms.WriteInt(RunCount);
                    ms.WriteInt(HeroList.Count);
                    for (int i = 0; i < HeroList.Count; i++)
                    {
                        ms.WriteInt(HeroList[i]);
                    }
                }
                else
                {
                    ms.WriteBool(false);
                }

                buffer = ms.ToArray();
            }
            return buffer;
        }

        public static ResOnline Decode(byte[] buffer)
        {
            ResOnline dto = new ResOnline();
            using (MemoryStreamExt ms = new MemoryStreamExt(buffer))
            {
                dto.HasUser = ms.ReadBool();
                if (dto.HasUser)
                {
                    dto.ID = ms.ReadInt();
                    dto.AccountID = ms.ReadInt();
                    dto.Name = ms.ReadUTF8String();

                    dto.Level = ms.ReadInt();
                    dto.Exp = ms.ReadInt();
                    dto.WinCount = ms.ReadInt();
                    dto.LoseCount = ms.ReadInt();
                    dto.RunCount = ms.ReadInt();
                    int heroCount = ms.ReadInt();
                    dto.HeroList = new List<int>();
                    for (int i = 0; i < heroCount; i++)
                    {
                        dto.HeroList.Add(ms.ReadInt());
                    }
                }

            }
            return dto;
        }
    }
}
